package com.ice.app.system.config.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.config.service.ConfigService;
import com.ice.app.system.entity.Config;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;

/**
 * 
 * 控制层： 系统配置信息表
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/sys/config")
public class ConfigController extends BaseController {

	@Autowired
	private ConfigService configService;

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getConfig(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Config config = configService.getConfig(key);
		return Result.success().add("config", config);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param config
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result configIndexData(Config config, Page<Config> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Config> configs = configService.getConfigs(config);
		PageInfo<Config> data = new PageInfo<Config>(configs, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param config
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveConfig(Config config) throws Exception {
		if (configService.saveConfig(config)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delConfig(@RequestParam(value = "keys") String keys) {
		if (configService.deleteConfigs(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 更新缓存
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/cache", method = RequestMethod.POST)
	public Result cacheConfig() {
		configService.cacheConfig();
		return Result.success();
	}

}